package com.hu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hu.common.common.HttpStatus;
import com.hu.common.controller.BaseController;
import com.hu.common.pojo.Role;
import com.hu.common.pojo.dto.QueryInfo;
import com.hu.common.pojo.dto.Response;
import com.hu.common.pojo.dto.TableDataInfo;
import com.hu.common.vo.RoleMenuVo;
import com.hu.user.service.IRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hu
 * @since 2023-08-01
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Resource
    private IRoleService roleService;

    // 新增或者更新
    @PostMapping("/save")
    public Response save(@RequestBody Role role) {
        return success(roleService.saveOrUpdate(role));
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {
        return success(roleService.removeById(id));
    }

    @PostMapping("/del/batch")
    public Response deleteBatch(@RequestBody List<Integer> ids) {
        return success(roleService.removeByIds(ids));
    }

    @GetMapping
    public Response findAll() {
        return success(roleService.list());
    }

    @GetMapping("/{id}")
    public Response findOne(@PathVariable Integer id) {
        return success(roleService.getById(id));
    }

    @PostMapping("/page2")
    public TableDataInfo findPage2(@RequestBody QueryInfo<Role> queryInfo) {
        startPage(queryInfo);
        return getDataTable(roleService.queryRole(queryInfo.getQueryObject(), queryInfo.getOrderStatus(),queryInfo.getColumnName()));
    }

    @GetMapping("/page")
    public Response findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String name,
                               @RequestParam(defaultValue = "") String description) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)){
            queryWrapper.like("name",name);
        }
        if (!"".equals(description)){
            queryWrapper.like("description",description);
        }
        return success(roleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


    @PostMapping("/roleMenu")
    public Response roleMenu(@RequestBody RoleMenuVo roleMenuVo) {
        String menuIds = roleMenuVo.getMenuIds().stream().map(String::valueOf).collect(Collectors.joining(","));
        roleService.setRoleMenu(roleMenuVo.getRoleId(), menuIds);
        return success(HttpStatus.SUCCESS, "角色分配成功");
    }

    @GetMapping("/roleMenu/{roleId}")
    public Response getMenuOfRole(@PathVariable Integer roleId) {
        return success("菜单获取成功",roleService.getMenuById(roleId));
    }
}
